<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use App\Exception\XZXValidationException;
use App\Middleware\AdminApi\AdminJWTAuthMiddleware;
use App\Middleware\AdminApi\NodeAccessMiddleware;
use App\Service\TradeTransReviewService;
use App\Service\TradeTransService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\Arr;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

/**
 * Class TradeTransController
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/tradeTrans")
 */
class TradeTransController extends AdminBaseController {
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @Inject()
     * @var TradeTransService
     */
    protected $TradeTranService;

    /**
     * @Inject()
     * @var TradeTransReviewService
     */
    protected $TradeTransReviewService;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 查看
     * @PostMapping(path = "get")
     * @Middleware(AdminJWTAuthMiddleware::class)
     */
    public function get (RequestInterface $request) {
        $validator = $this->validationFactory->make($request->all(), [
            'id' => 'required|uuid',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param                = $request->all();
        $ext                  = $this->request->getAttribute("ext");
        $param['isSuperRole'] = $ext['isSuperRole'];
        if (!$param['isSuperRole']) {
            $param['mchIdList'] = $ext['mchIdList'];
        }
        $param['source'] = 2;
        return $this->TradeTranService->get($param);
    }

    /**
     * 查询
     * @PostMapping(path = "list")
     * @Middleware(AdminJWTAuthMiddleware::class)
     */
    public function list (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'page'  => 'required|integer|min:1',
            'limit' => 'required|integer|min:1',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        CustomUtils::xzxDebug($param);
        if (isset($param['search'])) {
            $search = $param['search'];
            CustomUtils::xzxAddSearch($param, $search, 'type');
            CustomUtils::xzxAddSearch($param, $search, 'handleStatus');
            CustomUtils::xzxAddSearch($param, $search, 'mchNo');
            CustomUtils::xzxAddSearch($param, $search, 'createTimeRange');
            if (isset($param["createTimeRange"])) {
                try {
                    $param['createTimeRange'][0] = DateUtils::dateToTimeStamp($param['createTimeRange'][0]);
                    $param['createTimeRange'][1] = DateUtils::dateToTimeStamp($param['createTimeRange'][1]);
                } catch (\Throwable $th) {
                    unset($param['createTimeRange']);
                }
            }
        }
        $ext                  = $this->request->getAttribute("ext");
        $param['isSuperRole'] = $ext['isSuperRole'];
        if (!$param['isSuperRole']) {
            $param['mchIdList'] = $ext['mchIdList'];
        }

        return $this->TradeTranService->list($param);
    }

    /**
     * 提现/初审
     * @PostMapping(path = "withdrawFirstReview")
     * @Middlewares({
     *     @Middleware(AdminJWTAuthMiddleware::class),
     *     @Middleware(NodeAccessMiddleware::class)
     * })
     */
    public function withdrawFirstReview (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'id' => 'required|uuid',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $ext = $this->request->getAttribute("ext");

        $param = [
            'id'        => $request->post('id'),
            'isApprove' => (bool) $request->post('isApprove'),
            'remark'    => $request->post('remark'),
            'useApi'    => (bool) $request->post('useApi'),
            'channelId' => $request->post('channelId'),
        ];

        $param['source']      = 2;
        $param['isSuperRole'] = Arr::get($ext, 'isSuperRole', false);
        if (!$param['isSuperRole']) {
            $param['mchIdList'] = Arr::get($ext, 'mchIdList', []);
        }
        $param['createBy'] = Arr::get($ext, 'createBy');

        return $this->TradeTransReviewService->withdrawFirstReview($param);
    }

    /**
     * 提现/复审
     * @PostMapping(path = "withdrawSecondReview")
     * @Middlewares({
     *     @Middleware(AdminJWTAuthMiddleware::class),
     *     @Middleware(NodeAccessMiddleware::class)
     * })
     */
    public function withdrawSecondReview (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'id' => 'required|uuid',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $ext = $this->request->getAttribute("ext");

        $param = [
            'id'        => $request->post('id'),
            'isApprove' => (bool) $request->post('isApprove'),
            'remark'    => $request->post('remark'),
        ];

        $param['source']      = 2;
        $param['isSuperRole'] = Arr::get($ext, 'isSuperRole', false);
        if (!$param['isSuperRole']) {
            $param['mchIdList'] = Arr::get($ext, 'mchIdList', []);
        }
        $param['createBy'] = Arr::get($ext, 'createBy');

        return $this->TradeTransReviewService->withdrawSecondReview($param);
    }

    /**
     * 转帐/初审
     * @PostMapping(path = "transferFirstReview")
     * @Middlewares({
     *     @Middleware(AdminJWTAuthMiddleware::class),
     *     @Middleware(NodeAccessMiddleware::class)
     * })
     */
    public function transferFirstReview (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'id' => 'required|uuid',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $ext = $this->request->getAttribute("ext");

        $param = [
            'id'        => $request->post('id'),
            'isApprove' => (bool) $request->post('isApprove'),
            'remark'    => $request->post('remark'),
            'useApi'    => (bool) $request->post('useApi'),
            'channelId' => $request->post('channelId'),
        ];

        $param['source']      = 2;
        $param['isSuperRole'] = Arr::get($ext, 'isSuperRole', false);
        if (!$param['isSuperRole']) {
            $param['mchIdList'] = Arr::get($ext, 'mchIdList', []);
        }
        $param['createBy'] = Arr::get($ext, 'createBy');

        return $this->TradeTransReviewService->transferFirstReview($param);
    }

    /**
     * 转帐/复审
     * @PostMapping(path = "transferSecondReview")
     * @Middlewares({
     *     @Middleware(AdminJWTAuthMiddleware::class),
     *     @Middleware(NodeAccessMiddleware::class)
     * })
     */
    public function transferSecondReview (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'id' => 'required|uuid',
        ]);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $ext = $this->request->getAttribute("ext");

        $param = [
            'id'        => $request->post('id'),
            'isApprove' => (bool) $request->post('isApprove'),
            'remark'    => $request->post('remark'),
        ];

        $param['source']      = 2;
        $param['isSuperRole'] = Arr::get($ext, 'isSuperRole', false);
        if (!$param['isSuperRole']) {
            $param['mchIdList'] = Arr::get($ext, 'mchIdList', []);
        }
        $param['createBy'] = Arr::get($ext, 'createBy');

        return $this->TradeTransReviewService->transferSecondReview($param);
    }

}
